const { createClient } = require("redis");
const IDValidator = require("id-validator");

class UserGenerator {

    constructor(redisHost = "127.0.0.1", redisPort = 6379, redisDb = 0) {
        this.redisHost = redisHost;
        this.redisPort = redisPort;
        this.redisDb = redisDb;
    }

    async generate(number = 10000) {
        const client = await createClient({ host: this.redisHost, port: this.redisPort }).on("error", () => { }).connect();
        client.select(this.redisDb);
        client.del("users");
        const validator = new IDValidator();
        const prefixes = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
            "145", "147", "149", "150", "151", "152", "153", "155", "156", "157",
            "158", "159", "166", "170", "171", "172", "173", "175", "176", "177",
            "178", "180", "181", "182", "183", "184", "185", "186", "187", "188",
            "189", "191", "193", "195", "196", "198", "199"];
        const randomPrefix = prefixes[Math.floor(Math.random() * prefixes.length)];
        const randomNumber = Math.floor(Math.random() * 100).toString().padStart(2, "0");
        for (let i = 0; i < number; i++) {
            const fullName = `性能用户${randomPrefix}${randomNumber}${String(i + 1).padStart(6, "0")}`;
            const idcardNo = validator.makeID();
            const phone = `${randomPrefix}${randomNumber}${String(i + 1).padStart(6, "0")}`;
            const user = { fullName, idcardNo, phone };
            await client.rPush("users", JSON.stringify(user));
        }
        await client.disconnect();

    }

}

module.exports = UserGenerator;